//
//  NewFeatureController.swift
//  sinaweibo
//
//  Created by icefs2 on 15/11/13.
//  Copyright © 2015年 icefs2. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let imageCount = 4

class NewFeatureController: UICollectionViewController {
    init() {
        //实例化layout
        let layout = UICollectionViewFlowLayout()
        //设置layout的属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        //设置分页滚动
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

       
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
       cell.index = indexPath.item
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        if page == imageCount - 1 {
            //开始动画效果
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
                cell.startAnimation()
            }
        }
    }
}
//
class NewFeatureCell: UICollectionViewCell {
    var index: Int  = 0 {
        didSet{
           iconview.image = UIImage(named: "new_feature_\(index + 1)")
             startbutton.hidden = true
        }
    }
    //点击开始按钮的动画效果
    private func startAnimation() {
        
        startbutton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
           self.startbutton.hidden = false
            self.startbutton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                print("OK")
        }
    }
    
       @objc private func start() {
        print("OK")
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(iconview)
        contentView.addSubview(startbutton)
        iconview.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startbutton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        startbutton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
       
    }
    //懒加载子控件
    private lazy var iconview: UIImageView = UIImageView()
    private lazy var startbutton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        return btn
    }()
    
}
